/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.channel.service.impl;

import com.fanrenqiji.modules.sys.entity.User;
import com.fanrenqiji.modules.sys.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;

import com.fanrenqiji.entity.ChannelEntity;
import com.fanrenqiji.modules.business.channel.vo.ChannelVO;
import com.fanrenqiji.modules.business.channel.dao.ChannelDao;
import com.fanrenqiji.modules.business.channel.service.ChannelService;

import org.dozer.DozerBeanMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.fanrenqiji.common.persistence.Page;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Order;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 渠道来源业务逻辑实现
 *
 * @author wentao
 */
@Service(value = "channel")
@Transactional(readOnly = true)
public class ChannelServiceImpl implements ChannelService{

@Autowired
private ChannelDao channelDao;

@Autowired
    DozerBeanMapper beanMapper;

/**
 * 插入
 *
 * @param channel
 * 插入的对象
 * @return 插入数据条数
 */
@Transactional
public ChannelVO insert(ChannelVO channel){
        ChannelEntity channelEntity=new ChannelEntity();
        beanMapper.map(channel,channelEntity);
        channelDao.save(channelEntity);
        beanMapper.map(channelEntity,channel);
        return channel;
        }


/**
 * 批量插入
 *
 * @param list
 * 数据列表
 * @return 插入数据条数
 */
@Transactional
public void insertList(List<ChannelVO>list){
        for(ChannelVO channel:list){
        insert(channel);
        }
        }


/**
 * 修改
 *
 * @param channel
 *            修改的数据
 * @return 修改的数据
 */
@Transactional
public ChannelVO update(ChannelVO channel){
        ChannelEntity channelEntity=null;
        if(StringUtils.isNotEmpty(channel.getId())){
        channelEntity=channelDao.findOne(Long.parseLong(channel.getId()));
        }
        if(channelEntity==null){
        channelEntity=new ChannelEntity();
        }
        beanMapper.map(channel,channelEntity);
        if (StringUtils.isNotBlank(channel.getCompanyId())){
           channelEntity.setOffice(null);
           channelEntity.setCompanyId(Long.parseLong(channel.getCompanyId()));
        }
        channelDao.clear();
        channelDao.save(channelEntity);
        beanMapper.map(channelEntity,channel);
        return channel;
        }


/**
 * 删除
 *
 * @param id
 *            数据标识
 * @return 删除的数据条数
 */
@Transactional
public void delete(String id){
        channelDao.deleteById(Long.parseLong(id));
        }


/**
 * 查询所有数据
 *
 * @return 数据列表
 */
public List<ChannelVO>getAll(){
        List<ChannelVO>list=Lists.newArrayList();
        List<ChannelEntity>entities=channelDao.findAllList();
        for(ChannelEntity channelEntity:entities){
        ChannelVO channelVO=new ChannelVO();
        beanMapper.map(channelEntity,channelVO);
        list.add(channelVO);
        }
        return list;
        }


/**
 * 通过条件查询数据列表
 *
 * @param channel
 *            查询条件
 * @return 数据列表
 */
public List<ChannelVO>getList(ChannelVO channel){
        List<ChannelVO>list=Lists.newArrayList();
        DetachedCriteria dc=channelDao.createDetachedCriteria();
        dc.add(Restrictions.eq(ChannelEntity.DEL_FLAG,ChannelEntity.DEL_FLAG_NORMAL));
    /*
    dc.createAlias("channel", "channel"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=

    if (StringUtils.isNotEmpty(channel.getName())) {
        dc.add(Restrictions.like("name", "%" + channel.getName() + "%")); //like
    }
    if (StringUtils.isNotEmpty(channel.getName())) {
        dc.add(Restrictions.eq("name", channel.getName())); //=
    }

	dc.addOrder(Order.asc("office.id")).addOrder(Order.asc("name"));  //排序

    */
        List<ChannelEntity>entities=channelDao.find(dc);
        for(ChannelEntity channelEntity:entities){
        ChannelVO channelVO=new ChannelVO();
        beanMapper.map(channelEntity,channelVO);
        list.add(channelVO);
        }
        return list;
        }


/**
 * 通过id查询数据
 *
 * @param id
 *            数据标识
 * @return 数据对象
 */
public ChannelVO get(String id){
        ChannelVO channel=new ChannelVO();
        ChannelEntity channelEntity=channelDao.findOne(Long.parseLong(id));
        if(channelEntity==null){
        channelEntity=new ChannelEntity();
        }
        beanMapper.map(channelEntity,channel);
        return channel;
        }


/**
 * 根据条件分页查询
 * @param page
 * @param channel
 * @return
 */
public Page<ChannelVO>findChannelByPage(Page<ChannelVO>page,ChannelVO channel){
        Page<ChannelEntity>pageEntity=new Page<ChannelEntity>(page.getPageNo(),page.getPageSize());
        DetachedCriteria dc=channelDao.createDetachedCriteria();
        dc.add(Restrictions.eq(ChannelEntity.DEL_FLAG,ChannelEntity.DEL_FLAG_NORMAL));
        if (StringUtils.isNotEmpty(channel.getName())) {
            dc.add(Restrictions.like("name", "%" + channel.getName() + "%")); //like
        }

        dc.createAlias("office", "office");
        User currentUser = UserUtils.getUser();
        if (currentUser.getOffice() != null && currentUser.getOffice().getId() != null) {
                dc.add(Restrictions.or(
                        Restrictions.eq("office.id", currentUser.getOffice().getId()),
                        Restrictions.like("office.parentIds", "%,"
                                + currentUser.getOffice().getId() + ",%")));
        }
    /*
    dc.createAlias("channel", "channel"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=


    if (StringUtils.isNotEmpty(channel.getName())) {
        dc.add(Restrictions.eq("name", channel.getName())); //=
    }

    if (!StringUtils.isNotEmpty(page.getOrderBy())) { //排序
	    dc.addOrder(Order.asc("name"))
		  .addOrder(Order.desc("id"));
	}
    */
        if(!StringUtils.isNotEmpty(page.getOrderBy())){
            dc.addOrder(Order.desc("id"));
        }
        pageEntity=channelDao.find(pageEntity,dc);
        beanMapper.map(pageEntity,page);
        List<ChannelVO>list=Lists.newArrayList();
        for(ChannelEntity channelEntity:pageEntity.getList()){
            ChannelVO channelVO=new ChannelVO();
            beanMapper.map(channelEntity,channelVO);
            list.add(channelVO);
        }
        page.setList(list);
        return page;
        }


 }
